﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmViewProject : Form
    {
        public frmViewProject()
        {
            InitializeComponent();
        }
        SqlConnection con = Common.con;
        private void frmViewProject_Load(object sender, EventArgs e)
        {
            buidtree();
            btnEdit.Visible = true;
            btnCancel.Visible = false;
            btnDelete.Enabled = false;
            btnSave.Enabled = false;
            load(frmSearchProject.abc);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            btnDelete.Enabled = true;
            btnCancel.Visible = true;
            btnEdit.Visible = false;
            txtNameProject.Enabled = true;
            cmbCustomer.Enabled = true;
            dtpStartDate.Enabled = true;
            dtpEndDate.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtIdProject.Text == "" || txtNameProject.Text == "" || dtpStartDate.Text == "" || dtpEndDate.Text == "" || cmbCustomer.Text == "")
                {
                    MessageBox.Show("You must enter all infomation");
                }

                else
                {
                    if (dtpEndDate.Value.Date <= dtpStartDate.Value.Date)
                    {
                        MessageBox.Show("End Date must bigger than start date !!!!");
                    }
                    else
                    {
                        if(con.State!=ConnectionState.Open)
                            con.Open();
                        DateTime dt1 = dtpStartDate.Value;
                        DateTime dt2 = dtpEndDate.Value;
                        string sql = "UPDATE PROJECT SET NAME_PROJECT='" + txtNameProject.Text + "',ID_CUSTOMER='" + cmbCustomer.SelectedValue + "',START_DATE='" + dt1 + "',END_DATE='" + dt2 + "' WHERE ID_PROJECT='" + txtIdProject.Text + "'";
                        SqlCommand com = new SqlCommand(sql, con);
                        int res = com.ExecuteNonQuery();
                        con.Close();
                        if (res != 0)
                        {
                            MessageBox.Show("Project updated successfully !");
                            txtNameProject.Enabled = false;
                            cmbCustomer.Enabled = false;
                            dtpStartDate.Enabled = false;
                            dtpEndDate.Enabled = false;
                        }
                    }
                }

            }
            catch
            {
                MessageBox.Show("Problem !!!");
            }
        }


        public void load(string abcde)
        {
            try
            {
                control.Visible = true;
                if (con.State != ConnectionState.Open)
                    con.Open();
                string select = "SELECT TASK.* FROM ASSIGN_TASK_STATUS INNER JOIN ((PROJECT INNER JOIN TASK_GROUP ON PROJECT.ID_PROJECT = TASK_GROUP.ID_PROJECT) INNER JOIN (PRIORITY INNER JOIN TASK ON PRIORITY.ID_PRIORITY = TASK.ID_PRIORITY) ON TASK_GROUP.ID_TASK_GROUP = TASK.ID_TASK_GROUP) ON ASSIGN_TASK_STATUS.ID_TASK_STATUS = TASK.ID_TASK_STATUS WHERE (((PROJECT.ID_PROJECT)='" + abcde + "'))";
                SqlDataAdapter da = new SqlDataAdapter(select, con);
                DataTable ds = new DataTable();
                da.Fill(ds);
                con.Close();
                dgshow2.DataSource = ds;
                DataGridViewCellStyle style1 = new DataGridViewCellStyle();
                style1.ForeColor = Color.Blue;
                style1.BackColor = Color.AliceBlue;
                for (int i = dgshow2.RowCount - 1; i >= 0; i--)
                {
                    if (i % 2 == 0) dgshow2.Rows[i].DefaultCellStyle = style1;
                }

                //Dua du lieu vao Tab Control
                txtNameProject.DataBindings.Clear();
                txtIdProject.DataBindings.Clear();
                cmbCustomer.DataBindings.Clear();
                dtpStartDate.DataBindings.Clear();
                dtpEndDate.DataBindings.Clear();
                dgshow1.DataBindings.Clear();
                dgshow2.DataBindings.Clear();

                SqlDataAdapter adap1 = new SqlDataAdapter();
                SqlCommand com = new SqlCommand();
                DataSet ds1 = new DataSet();
                string sql = "SELECT * FROM CUSTOMER";
                com = new SqlCommand(sql, con);
                adap1 = new SqlDataAdapter(com);
                ds1 = new DataSet();
                adap1.Fill(ds1, "CUSTOMER");
                DataTable dt1 = ds1.Tables["CUSTOMER"];

                cmbCustomer.DataSource = dt1;
                cmbCustomer.DisplayMember = "NAME_CUSTOMER";
                cmbCustomer.ValueMember = "ID_CUSTOMER";




                string sqlstring = "select PROJECT.ID_PROJECT, PROJECT.NAME_PROJECT, CUSTOMER.NAME_CUSTOMER, PROJECT.START_DATE, PROJECT.END_DATE from PROJECT inner join CUSTOMER on PROJECT.ID_CUSTOMER = CUSTOMER.ID_CUSTOMER where PROJECT.ID_PROJECT='" + abcde + "'";

                SqlDataAdapter da1 = new SqlDataAdapter(sqlstring, con);
                DataTable table = new DataTable();
                da1.Fill(table);
                BindingSource bdsource = new BindingSource();
                bdsource.DataSource = table;
                txtIdProject.DataBindings.Add("Text", bdsource, "ID_PROJECT");
                txtIdProject.Enabled = false;
                txtNameProject.DataBindings.Add("Text", bdsource, "NAME_PROJECT");
                txtNameProject.Enabled = false;
                cmbCustomer.DataBindings.Add("Text", bdsource, "NAME_CUSTOMER");
                cmbCustomer.Enabled = false;
                dtpStartDate.DataBindings.Add("Value", bdsource, "START_DATE");
                dtpStartDate.Enabled = false;
                dtpEndDate.DataBindings.Add("Value", bdsource, "END_DATE");
                dtpEndDate.Enabled = false;

                con.Close();
            }
            catch
            {
                MessageBox.Show("Problem!!!");
            }
        
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnCancel.Visible = false;
            btnSave.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Visible = true;
            txtNameProject.Enabled = false;
            cmbCustomer.Enabled = false;
            dtpEndDate.Enabled = false;
            dtpStartDate.Enabled = false;
        }
        public void buidtree()
        {
            try
            {
                DataTable messages = GetTreeViewData();
                DataView threads = GetThreads(messages);
                foreach (DataRowView row in threads)
                {
                    TreeNode threadNode = new TreeNode();
                    threadNode.Text = row["NAME_PROJECT"].ToString();
                    threadNode.Name = row["ID_PROJECT"].ToString();

                }
            }
            catch
            {
                MessageBox.Show("Problem !!!");
            }
        }

        private DataTable GetTreeViewData()
        {
            if (con.State != ConnectionState.Open)
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM PROJECT", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataTable dtbl = new DataTable();
            ad.Fill(dtbl);
            con.Close();
            return dtbl;
        }

        DataView GetThreads(DataTable discuss)
        {
            DataView view = new DataView(discuss);
            view.RowFilter = "[PARENT]=1";
            return view;
        }

        


    }
}
